#include <QFormLayout>
#include <QMessageBox>
#include <QLabel>
#include <QGridLayout>
#include <QFileDialog>

#include "VideoNewDlg.hxx"

#pragma execution_character_set("utf-8")

VideoNewDialog::VideoNewDialog()
{
    _videoInfo = NULL;

    setWindowTitle("Add Video for the Project");

    QVBoxLayout* vlayout = new QVBoxLayout();

    QGridLayout* gridLayout = new QGridLayout();
    //gridLayout->setSizeConstraint()

    _edtVideoName = new QLineEdit();
    //_edtVideoName->setMaximumWidth(160);
    _lblVideoFile = new QLabel("Empty");

    _cbVideoType = new QComboBox();
    _cbVideoType->addItem(tr("Video File"));
    //_cbVideoType->addItem(tr("Image Sequence"));
   // _cbVideoType->setMaximumWidth(160);

    _sbFrameStrip = new QSpinBox();
    _sbFrameStrip->setMinimum(1);
    _sbFrameStrip->setMaximum(1000);
    _sbFrameStrip->setMaximumWidth(80);

    _btnVideoFile = new QPushButton(tr("Select Video File"));
    _btnVideoFile->setMaximumWidth(180);

    _btnAdd = new QPushButton(tr("Add"));
    _btnAdd->setMaximumWidth(120);

    _btnCancel = new QPushButton(tr("Cancel"));
    _btnCancel->setMaximumWidth(120);

    connect(_btnAdd, SIGNAL(clicked()), this, SLOT(onBtnAdd()));
    connect(_btnCancel, SIGNAL(clicked()), this, SLOT(onBtnCancel()));
    connect(_btnVideoFile, SIGNAL(clicked()), this, SLOT(onBtnVideoFile()));

    gridLayout->addWidget(new QLabel("Video Name:"), 0, 0);
    gridLayout->addWidget(_edtVideoName, 0, 1);
    gridLayout->addWidget(new QLabel("Video Type:"), 1, 0);
    gridLayout->addWidget(_cbVideoType, 1, 1);
    gridLayout->addWidget(new QLabel("Frame Strip:"),2,0);
    gridLayout->addWidget(_sbFrameStrip, 2, 1);
    gridLayout->addWidget(new QLabel("Video File:"), 3, 0);
    gridLayout->addWidget(_lblVideoFile, 3, 1);
    gridLayout->addWidget(_btnVideoFile, 4, 1);


    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->addWidget(_btnAdd);
    hlayout->addWidget(_btnCancel);

    vlayout->addLayout(gridLayout);


    vlayout->addStretch();
    vlayout->addLayout(hlayout);

    vlayout->setSpacing(20);
    vlayout->setMargin(30);

    setLayout(vlayout);

    setMinimumWidth(500);
    setMinimumHeight(350);
}

void VideoNewDialog::onBtnCancel()
{
    reject();
}

void VideoNewDialog::onBtnAdd()
{
    if (_edtVideoName->text().trimmed().length() > 0)
        _videoInfo->VideoName = _edtVideoName->text().trimmed();
    else
    {
        QMessageBox::warning(this, "Warnning", tr("Please Input Valid Name for the Video!"));
        return;
    }

    if (_lblVideoFile->text().trimmed().length() > 0 && QString::compare(_lblVideoFile->text(), "Empty") != 0)
        _videoInfo->VideoFile = _lblVideoFile->text().trimmed();
    else
    {
        QMessageBox::warning(this, "Warnning", tr("Please Select an Valid Video File!"));
        return;
    }

    _videoInfo->VideoType = VideoType::VT_VIDEO;
    _videoInfo->FrameStrip = _sbFrameStrip->value();

    accept();
}

void VideoNewDialog::onBtnVideoFile()
{
    assert(_videoInfo);

    QString fileName = QFileDialog::getOpenFileName(this,
        		tr("Select a Video for the Project!"), ".", tr("Video Files (*.mp4 *.avi *.mpeg *.h264 *.webm)"));

    if (fileName.trimmed().length() > 0)
    {
        QString filePath = fileName.trimmed();
        _lblVideoFile->setText(filePath);

        if (_edtVideoName->text().trimmed().length() <= 0)
        {
            int slashPos = filePath.lastIndexOf(QChar('/'));
            int dotPos = filePath.lastIndexOf(QChar('.'));

            QString videoName = filePath.mid(slashPos + 1, dotPos - slashPos - 1);
            _edtVideoName->setText(videoName);
        }
    }
}
